package cn.edu.hust.sse.software7.class3.DAO;

import java.util.List;
import java.util.logging.Logger;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.jdo.Transaction;

import cn.edu.hust.sse.software7.class3.common.PMF;
import cn.edu.hust.sse.software7.class3.entities.EBCommonOnProduct;

public class EBCommonOnProductDAO {
	private static final Logger log = Logger
			.getLogger(EBCommonOnProductDAO.class.getName());

	public void addCommon(EBCommonOnProduct common) {
		log.info("add common on product " + common.getCommonProdId()
				+ " begin.");
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Transaction tx = pm.currentTransaction();
		try {
			tx.begin();
			pm.makePersistent(common);
			tx.commit();
			log.finest("add common on product " + common.getCommonProdId()
					+ " success.");
		} catch (Exception e) {
			log.info("add common on product " + common.getCommonProdId()
					+ " failed!!!\n" + e.getStackTrace());
		} finally {
			if (tx.isActive()) {
				tx.rollback();
			}
		}
	}

	public List<EBCommonOnProduct> getCommonsByProductId(Long prodId) {
		log.info("Get commons of Product " + prodId + " begin");
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Transaction tx = pm.currentTransaction();
		List<EBCommonOnProduct> commons = null;
		try {
			tx.begin();
			Query query = pm.newQuery(EBCommonOnProduct.class);
			query.setFilter("commonProdId == " + prodId + "");
			commons = (List<EBCommonOnProduct>) query.execute();
			tx.commit();
			log.finest("Get commons of Product " + prodId + " finished");
		} catch (Exception e) {
			log.info("Get commons of Product " + prodId + " occurred errors.");
		} finally {
			if (tx.isActive()) {
				tx.rollback();
			}
		}
		return commons;
	}

	public List<EBCommonOnProduct> getCommonsByProdUser(Long prodId, Long userId) {
		log.info("get Commons By Prod " + prodId + " and User " + userId
				+ " begin.");
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Transaction tx = pm.currentTransaction();
		List<EBCommonOnProduct> commons = null;
		try {
			tx.begin();
			Query query = pm.newQuery(EBCommonOnProduct.class);
			query.setFilter("commonProdId == '" + prodId + "'");
			query.setFilter("commonerId == '" + userId + "'");
			commons = (List<EBCommonOnProduct>) query.execute();
			tx.commit();
			log.finest("get Commons By Prod " + prodId + " and User " + userId
					+ " finished.");
		} catch (Exception e) {
			log.info("get Commons By Prod " + prodId + " and User " + userId
					+ " occurred errors.");
		} finally {
			if (tx.isActive()) {
				tx.rollback();
			}
		}
		return commons;
	}

	public void deleteCommonsByProd(Long prodId) {
		log.info("delete Commons By Prod " + prodId + " begin.");
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Transaction tx = pm.currentTransaction();
		try {
			tx.begin();
			Query query = pm.newQuery(EBCommonOnProduct.class);
			query.setFilter("commonProdId == '" + prodId + "'");
			List<EBCommonOnProduct> commons = (List<EBCommonOnProduct>) query.execute();
			pm.deletePersistentAll(commons);
			tx.commit();
			log.finest("delete Commons By Prod " + prodId + " finished");
		} catch (Exception e) {
			log.info("delete Commons By Prod " + prodId + " occurred errors.");
		} finally {
			if (tx.isActive()) {
				tx.rollback();
			}
		}

	}

	public void deleteCommonsByUser(Long userId) {
		log.info("delete Commons By User " +userId + " begin.");
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Transaction tx = pm.currentTransaction();
		try {
			tx.begin();
			Query query = pm.newQuery(EBCommonOnProduct.class);
			query.setFilter("commonerId == '" + userId + "'");
			List<EBCommonOnProduct> commons = (List<EBCommonOnProduct>) query.execute();
			pm.deletePersistentAll(commons);
			tx.commit();
			log.finest("delete Commons By User " +userId + " finished");
		} catch (Exception e) {
			log.info("delete Commons By User " +userId + " occurred errors.");
		} finally {
			if (tx.isActive()) {
				tx.rollback();
			}
		}
	}

}
